package com.jhy.guavacase.io;

import com.google.common.collect.ImmutableList;
import com.google.common.io.CharSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * @author jhy
 * @ClassName CharSourceCase.java
 * @Description CharSource的使用示例
 * @createTime 2022年03月14日 15:11:00
 */
public class CharSourceCase {


    //1.CharSource的read()方法：将该CharSource中的所有字符读为字符串
    //readLines方法是读取所有内容，并按行存到list中
    @Test
    public void testCharSourceRead() throws IOException {

        String content = "a\nb";
        //构造CharSource
        CharSource charSource = CharSource.wrap(content);
        //断言该CharSource
        Assertions.assertEquals(charSource.isEmpty(),false);
        Assertions.assertEquals(charSource.length(),3L);
        Assertions.assertEquals(charSource.lengthIfKnown().get(),3L);
        //read读取所有
        String resString = charSource.read();
        Assertions.assertEquals(resString,content);
        //readLines按行读取
        ImmutableList<String> resList = charSource.readLines();
        Assertions.assertEquals(resList.size(),2);
        Assertions.assertEquals(resList.get(0),"a");
        Assertions.assertEquals(resList.get(1),"b");
    }

    //2.测试concat将多个charSource合并为一个charSource
    @Test
    public void testCharSourceConcat() throws IOException {
        CharSource charSource1 = CharSource.wrap("1111");
        CharSource charSource2 = CharSource.wrap("2222");

        CharSource charSource = CharSource.concat(charSource1, charSource2);
        String read = charSource.read();
        Assertions.assertEquals(read,"11112222");
    }

}
